/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.taglib.core.grid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * <p>Adds additional parameter to query that will be constructed to get rows
 * of grid.
 * <br />
 * This tag is only valid when nested within <em>grid</em> tag.
 * </p>
 * <p>
 * Allowed attributes are:
 * <ul>
 * <li>
 * <b>name</b> - required - name of query parameter to be added
 * </li>
 * <li>
 * <b>value</b> - required - value of query parameter to be added
 * </li>
 * </ul>
 * </p>
 * <p><a href="QueryParameterTag.java.html"><i>View Source</i></a></p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.9 $ $Date: 2006/08/03 10:08:20 $
 * @jsp.tag name="queryParameter"
 * body-content="empty"
 */
public class QueryParameterTag extends SimpleTagSupport {

	protected transient final Log log = LogFactory.getLog(QueryParameterTag.class);

	/**
	 * Name of query parameter
	 */
	protected String name;

	/**
	 * Parameter value
	 */
	protected Object value;

	/**
     * Returns name of parameter
     *
	 * @return parameter name
	 * @jsp.attribute required="true"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="Name of query parameter"
	 */
	public String getName() {
		return name;
	}

    /**
     * Sets name of parameter
     *
     * @param name parameter name to set
     */
	public void setName(String name) {
		this.name = name;
	}

	/**
     * Returns value of parameter
     *
     * @return parameter value
	 * @jsp.attribute required="true"
	 * rtexprvalue="true"
	 * type="java.lang.Object"
	 * description="Parameter value"
	 */
	public Object getValue() {
		return value;
	}

    /**
     * Sets value of parameter
     *
     * @param value parameter value to set
     */
	public void setValue(Object value) {
		this.value = value;
	}

    /**
     * Processes the tag
     *
     * @throws JspTagException
     */
	public void doTag() throws JspTagException {

		PageContext pageContext = (PageContext) getJspContext();

		GridTag parentGridTag = (GridTag) findAncestorWithClass(this, GridTag.class);

		if ( parentGridTag == null ) {
			JspTagException e = new JspTagException("Parent tag is invalid! This tag is only valid when nested within 'grid' tag");
			throw e;
		}

		parentGridTag.getGrid().addQueryParameter(name, value);
	}

}
